package com.hty.filemanager;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.Formatter;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CategoryActivity extends AppCompatActivity {

    SimpleDateFormat SDF = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    String filepath = "";
    List<Cursor> list_cursor = new ArrayList<>();
    List<SimpleCursorAdapter> list_adapter = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        LinearLayout linearLayout_main = findViewById(R.id.linearLayout_main);
        Intent intent = getIntent();
        String category = intent.getStringExtra("Category");
        if (category.equals("Video")) {
            setTitle("视频");
            String[] projection = new String[]{ MediaStore.Video.Media.DATA , MediaStore.Video.Media.DATE_MODIFIED, MediaStore.Video.Media.SIZE, MediaStore.Video.Media._ID };
            Cursor cursor1 = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Video.Media.DATE_TAKEN + " DESC");
            ListView listView = new ListView(this);
            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor1, new String[] { MediaStore.Video.Media.DATA, MediaStore.Video.Media.DATA, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DATE_MODIFIED, MediaStore.Video.Media._ID }, new int[] { R.id.textView_path, R.id.textView_name, R.id.textView_size, R.id.textView_time, R.id.imageView_icon }, CursorAdapter.FLAG_AUTO_REQUERY);
            list_cursor.add(cursor1);
            list_adapter.add(adapter);
            adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
                public boolean setViewValue(View view, Cursor cursor, int columnIndex){
                    //Log.e(Thread.currentThread().getStackTrace()[2] + "", view.toString() + columnIndex);
                    if (view.getId() == R.id.textView_name) {
                        String path = cursor.getString(columnIndex);
                        ((TextView)view).setText(path.substring(path.lastIndexOf("/") + 1));
                        return true;
                    } else if (view.getId() == R.id.textView_size){
                        long filesize = cursor.getLong(columnIndex);
                        ((TextView)view).setText(Formatter.formatFileSize(CategoryActivity.this, filesize));
                        return true;
                    } else if (view.getId() == R.id.textView_time) {
                        Date date = new Date(cursor.getLong(columnIndex) * 1000);
                        ((TextView)view).setText(SDF.format(date));
                        return true;
                    } else if(view.getId() == R.id.imageView_icon){
                        int id = cursor.getInt(columnIndex);
                        Bitmap bitmap = MediaStore.Video.Thumbnails.getThumbnail(getContentResolver(), id, MediaStore.Video.Thumbnails.MINI_KIND, null);
                        ((ImageView)view).setImageBitmap(bitmap);
                        return true;
                    }
                    return false;
                }
            });
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    String path = ((TextView) arg1.findViewById(R.id.textView_path)).getText().toString();
                    String mime = URLConnection.getFileNameMap().getContentTypeFor(path);
                    if (mime != null) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.fromFile(new File(path)), mime);
                        startActivity(intent);
                    }
                }
            });
            listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
                @Override
                public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
                    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
                    filepath = ((TextView) info.targetView.findViewById(R.id.textView_path)).getText().toString();
                    String title = ((TextView) info.targetView.findViewById(R.id.textView_name)).getText().toString();
                    menu.setHeaderTitle(title);
                    menu.add(0, 0, 0, "复制");
                    menu.add(0, 1, 1, "剪切");
                    menu.add(0, 2, 2, "重命名");
                    menu.add(0, 3, 3, "属性");
                    menu.add(0, 4, 4, "分享");
                    menu.add(0, 5, 5, "删除");
                    menu.add(0, 6, 6, "打开文件所在位置");
                }
            });
            linearLayout_main.addView(listView);
        } else if (category.equals("Audio")) {
            setTitle("音频");
            String[] projection = new String[]{ MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.DATE_MODIFIED, MediaStore.Audio.Media.SIZE, MediaStore.Audio.Media._ID };
            Cursor cursor1 = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Audio.Media.DATE_MODIFIED + " DESC");
            ListView listView = new ListView(this);
            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor1, new String[]{ MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.SIZE, MediaStore.Audio.Media.DATE_MODIFIED, MediaStore.Audio.Media._ID }, new int[]{ R.id.textView_path, R.id.textView_name, R.id.textView_size, R.id.textView_time, R.id.imageView_icon }, CursorAdapter.FLAG_AUTO_REQUERY);
            list_cursor.add(cursor1);
            list_adapter.add(adapter);
            adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
                public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                    if (view.getId() == R.id.textView_name) {
                        String path = cursor.getString(columnIndex);
                        ((TextView) view).setText(path.substring(path.lastIndexOf("/") + 1));
                        return true;
                    } else if (view.getId() == R.id.textView_size) {
                        long filesize = cursor.getLong(columnIndex);
                        ((TextView) view).setText(Formatter.formatFileSize(CategoryActivity.this, filesize));
                        return true;
                    } else if (view.getId() == R.id.textView_time) {
                        Date date = new Date(cursor.getLong(columnIndex) * 1000);
                        ((TextView) view).setText(SDF.format(date));
                        return true;
                    } else if (view.getId() == R.id.imageView_icon) {
                        ((ImageView) view).setImageResource(R.mipmap.audiofile);
                        return true;
                    }
                    return false;
                }
            });
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    String path = ((TextView) arg1.findViewById(R.id.textView_path)).getText().toString();
                    String mime = URLConnection.getFileNameMap().getContentTypeFor(path);
                    Log.e(Thread.currentThread().getStackTrace()[2] + ": ", path + " -> " + mime);
                    if (mime != null) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.fromFile(new File(path)), mime);
                        startActivity(intent);
                    }
                }
            });
            listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
                @Override
                public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
                    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
                    filepath = ((TextView) info.targetView.findViewById(R.id.textView_path)).getText().toString();
                    String title = ((TextView) info.targetView.findViewById(R.id.textView_name)).getText().toString();
                    menu.setHeaderTitle(title);
                    menu.add(0, 0, 0, "复制");
                    menu.add(0, 1, 1, "剪切");
                    menu.add(0, 2, 2, "重命名");
                    menu.add(0, 3, 3, "属性");
                    menu.add(0, 4, 4, "分享");
                    menu.add(0, 5, 5, "删除");
                    menu.add(0, 6, 6, "打开文件所在位置");
                }
            });
            linearLayout_main.addView(listView);
        } else if (category.equals("Image")) {
            setTitle("图片");
            String[] projection = new String[]{ MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_MODIFIED, MediaStore.Images.Media.SIZE, MediaStore.Images.Media._ID };
            Cursor cursor1 = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Media.DATE_MODIFIED + " DESC");
            ListView listView = new ListView(this);
            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor1, new String[]{ MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATA, MediaStore.Images.Media.SIZE, MediaStore.Images.Media.DATE_MODIFIED, MediaStore.Images.Media._ID }, new int[]{ R.id.textView_path, R.id.textView_name, R.id.textView_size, R.id.textView_time, R.id.imageView_icon }, CursorAdapter.FLAG_AUTO_REQUERY);
            list_cursor.add(cursor1);
            list_adapter.add(adapter);
            adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
                public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                    if (view.getId() == R.id.textView_name) {
                        String path = cursor.getString(columnIndex);
                        ((TextView) view).setText(path.substring(path.lastIndexOf("/") + 1));
                        return true;
                    } else if (view.getId() == R.id.textView_size) {
                        long filesize = cursor.getLong(columnIndex);
                        ((TextView) view).setText(Formatter.formatFileSize(CategoryActivity.this, filesize));
                        return true;
                    } else if (view.getId() == R.id.textView_time) {
                        Date date = new Date(cursor.getLong(columnIndex) * 1000);
                        ((TextView) view).setText(SDF.format(date));
                        return true;
                    } else if (view.getId() == R.id.imageView_icon) {
                        int id = cursor.getInt(columnIndex);
                        Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), id, MediaStore.Images.Thumbnails.MINI_KIND, null);
                        ((ImageView) view).setImageBitmap(bitmap);
                        return true;
                    }
                    return false;
                }
            });
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    String path = ((TextView) arg1.findViewById(R.id.textView_path)).getText().toString();
                    String mime = URLConnection.getFileNameMap().getContentTypeFor(path);
                    Log.e(Thread.currentThread().getStackTrace()[2] + ": ", path + " -> " + mime);
                    if (mime != null) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.fromFile(new File(path)), mime);
                        startActivity(intent);
                    }
                }
            });
            listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
                @Override
                public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
                    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
                    filepath = ((TextView) info.targetView.findViewById(R.id.textView_path)).getText().toString();
                    String title = ((TextView) info.targetView.findViewById(R.id.textView_name)).getText().toString();
                    menu.setHeaderTitle(title);
                    menu.add(0, 0, 0, "复制");
                    menu.add(0, 1, 1, "剪切");
                    menu.add(0, 2, 2, "重命名");
                    menu.add(0, 3, 3, "属性");
                    menu.add(0, 4, 4, "分享");
                    menu.add(0, 5, 5, "删除");
                    menu.add(0, 6, 6, "打开文件所在位置");
                }
            });
            linearLayout_main.addView(listView);
        } else if (category.equals("Doc")) {
            setTitle("文档");
            TabHost tabHost = findViewById(R.id.tabHost);
            tabHost.setVisibility(View.VISIBLE);
            tabHost.setup();
            tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("txt", null).setContent(R.id.tab1));
            tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("html", null).setContent(R.id.tab2));
            tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("doc", null).setContent(R.id.tab3));
            tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator("pdf", null).setContent(R.id.tab4));
            tabHost.addTab(tabHost.newTabSpec("tab5").setIndicator("xls", null).setContent(R.id.tab5));
            tabHost.addTab(tabHost.newTabSpec("tab6").setIndicator("ppt", null).setContent(R.id.tab6));
            tabHost.setVisibility(View.VISIBLE);
            TabWidget tabWidget = tabHost.getTabWidget();
            int cc = tabWidget.getChildCount();
            for (int i=0; i<cc; i++) {
                tabWidget.getChildAt(i).getLayoutParams().width = 220;
                tabWidget.getChildAt(i).getLayoutParams().height = 120;
            }

            ListView listView_txt = findViewById(R.id.listView_txt);
            String[] projection = new String[]{ MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.DATE_MODIFIED, MediaStore.Files.FileColumns.SIZE, MediaStore.Files.FileColumns._ID };
            Cursor cursor1 = getContentResolver().query(MediaStore.Files.getContentUri("external"), projection, MediaStore.Files.FileColumns.MIME_TYPE + "=?", new String[]{ "text/plain" }, MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC");
            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor1, new String[]{ MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.SIZE, MediaStore.Files.FileColumns.DATE_MODIFIED, MediaStore.Files.FileColumns._ID }, new int[]{ R.id.textView_path, R.id.textView_name, R.id.textView_size, R.id.textView_time, R.id.imageView_icon }, CursorAdapter.FLAG_AUTO_REQUERY);
            list_cursor.add(cursor1);
            list_adapter.add(adapter);
            adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
                public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                    if (view.getId() == R.id.textView_name) {
                        String path = cursor.getString(columnIndex);
                        ((TextView) view).setText(path.substring(path.lastIndexOf("/") + 1));
                        return true;
                    } else if (view.getId() == R.id.textView_size) {
                        long filesize = cursor.getLong(columnIndex);
                        ((TextView) view).setText(Formatter.formatFileSize(CategoryActivity.this, filesize));
                        return true;
                    } else if (view.getId() == R.id.textView_time) {
                        Date date = new Date(cursor.getLong(columnIndex) * 1000);
                        ((TextView) view).setText(SDF.format(date));
                        return true;
                    } else if (view.getId() == R.id.imageView_icon) {
                        ((ImageView) view).setImageResource(R.mipmap.txtfile);
                        return true;
                    }
                    return false;
                }
            });
            listView_txt.setAdapter(adapter);
            listView_txt.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    String path = ((TextView) arg1.findViewById(R.id.textView_path)).getText().toString();
                    String mime = URLConnection.getFileNameMap().getContentTypeFor(path);
                    if (mime != null) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.fromFile(new File(path)), mime);
                        startActivity(intent);
                    }
                }
            });
            listView_txt.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
                @Override
                public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
                    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
                    filepath = ((TextView) info.targetView.findViewById(R.id.textView_path)).getText().toString();
                    String title = ((TextView) info.targetView.findViewById(R.id.textView_name)).getText().toString();
                    menu.setHeaderTitle(title);
                    menu.add(0, 0, 0, "复制");
                    menu.add(0, 1, 1, "剪切");
                    menu.add(0, 2, 2, "重命名");
                    menu.add(0, 3, 3, "属性");
                    menu.add(0, 4, 4, "分享");
                    menu.add(0, 5, 5, "删除");
                    menu.add(0, 6, 6, "打开文件所在位置");
                    menu.add(0, 7, 7, "编辑");
                }
            });

            ListView listView_htm = findViewById(R.id.listView_htm);
            cursor1 = getContentResolver().query(MediaStore.Files.getContentUri("external"), projection, MediaStore.Files.FileColumns.MIME_TYPE + "=?", new String[]{ "text/html" }, MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC");
            adapter = new SimpleCursorAdapter(this, R.layout.item, cursor1, new String[]{ MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.SIZE, MediaStore.Files.FileColumns.DATE_MODIFIED, MediaStore.Files.FileColumns._ID }, new int[]{R.id.textView_path, R.id.textView_name, R.id.textView_size, R.id.textView_time, R.id.imageView_icon}, CursorAdapter.FLAG_AUTO_REQUERY);
            list_cursor.add(cursor1);
            list_adapter.add(adapter);
            adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
                public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                    if (view.getId() == R.id.textView_name) {
                        String path = cursor.getString(columnIndex);
                        ((TextView) view).setText(path.substring(path.lastIndexOf("/") + 1));
                        return true;
                    } else if (view.getId() == R.id.textView_size) {
                        long filesize = cursor.getLong(columnIndex);
                        ((TextView) view).setText(Formatter.formatFileSize(CategoryActivity.this, filesize));
                        return true;
                    } else if (view.getId() == R.id.textView_time) {
                        Date date = new Date(cursor.getLong(columnIndex) * 1000);
                        ((TextView) view).setText(SDF.format(date));
                        return true;
                    } else if (view.getId() == R.id.imageView_icon) {
                        ((ImageView) view).setImageResource(R.mipmap.htmlfile);
                        return true;
                    }
                    return false;
                }
            });
            listView_htm.setAdapter(adapter);
            listView_htm.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    String path = ((TextView) arg1.findViewById(R.id.textView_path)).getText().toString();
                    String mime = URLConnection.getFileNameMap().getContentTypeFor(path);
                    if (mime != null) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.fromFile(new File(path)), mime);
                        startActivity(intent);
                    }
                }
            });
            listView_htm.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
                @Override
                public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
                    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
                    filepath = ((TextView) info.targetView.findViewById(R.id.textView_path)).getText().toString();
                    String title = ((TextView) info.targetView.findViewById(R.id.textView_name)).getText().toString();
                    menu.setHeaderTitle(title);
                    menu.add(0, 0, 0, "复制");
                    menu.add(0, 1, 1, "剪切");
                    menu.add(0, 2, 2, "重命名");
                    menu.add(0, 3, 3, "属性");
                    menu.add(0, 4, 4, "分享");
                    menu.add(0, 5, 5, "删除");
                    menu.add(0, 6, 6, "打开文件所在位置");
                    menu.add(0, 7, 7, "编辑");
                }
            });

            ListView listView_doc = findViewById(R.id.listView_file);
            cursor1 = getContentResolver().query(MediaStore.Files.getContentUri("external"), projection, MediaStore.Files.FileColumns.MIME_TYPE + "=?", new String[]{ "application/msword" }, MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC");
            adapter = new SimpleCursorAdapter(this, R.layout.item, cursor1, new String[]{ MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.SIZE, MediaStore.Files.FileColumns.DATE_MODIFIED, MediaStore.Files.FileColumns._ID }, new int[]{R.id.textView_path, R.id.textView_name, R.id.textView_size, R.id.textView_time, R.id.imageView_icon}, CursorAdapter.FLAG_AUTO_REQUERY);
            list_cursor.add(cursor1);
            list_adapter.add(adapter);
            adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
                public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                    if (view.getId() == R.id.textView_name) {
                        String path = cursor.getString(columnIndex);
                        ((TextView) view).setText(path.substring(path.lastIndexOf("/") + 1));
                        return true;
                    } else if (view.getId() == R.id.textView_size) {
                        long filesize = cursor.getLong(columnIndex);
                        ((TextView) view).setText(Formatter.formatFileSize(CategoryActivity.this, filesize));
                        return true;
                    } else if (view.getId() == R.id.textView_time) {
                        Date date = new Date(cursor.getLong(columnIndex) * 1000);
                        ((TextView) view).setText(SDF.format(date));
                        return true;
                    } else if (view.getId() == R.id.imageView_icon) {
                        ((ImageView) view).setImageResource(R.mipmap.docfile);
                        return true;
                    }
                    return false;
                }
            });
            listView_doc.setAdapter(adapter);
            listView_doc.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    String path = ((TextView) arg1.findViewById(R.id.textView_path)).getText().toString();
                    String mime = URLConnection.getFileNameMap().getContentTypeFor(path);
                    if (mime != null) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.fromFile(new File(path)), mime);
                        startActivity(intent);
                    }
                }
            });
            listView_doc.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
                @Override
                public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
                    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
                    filepath = ((TextView) info.targetView.findViewById(R.id.textView_path)).getText().toString();
                    String title = ((TextView) info.targetView.findViewById(R.id.textView_name)).getText().toString();
                    menu.setHeaderTitle(title);
                    menu.add(0, 0, 0, "复制");
                    menu.add(0, 1, 1, "剪切");
                    menu.add(0, 2, 2, "重命名");
                    menu.add(0, 3, 3, "属性");
                    menu.add(0, 4, 4, "分享");
                    menu.add(0, 5, 5, "删除");
                    menu.add(0, 6, 6, "打开文件所在位置");
                }
            });

            ListView listView_pdf = findViewById(R.id.listView_pdf);
            cursor1 = getContentResolver().query(MediaStore.Files.getContentUri("external"), projection, MediaStore.Files.FileColumns.MIME_TYPE + "=?", new String[]{ "application/pdf" }, MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC");
            adapter = new SimpleCursorAdapter(this, R.layout.item, cursor1, new String[]{ MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.SIZE, MediaStore.Files.FileColumns.DATE_MODIFIED, MediaStore.Files.FileColumns._ID }, new int[]{R.id.textView_path, R.id.textView_name, R.id.textView_size, R.id.textView_time, R.id.imageView_icon}, CursorAdapter.FLAG_AUTO_REQUERY);
            list_cursor.add(cursor1);
            list_adapter.add(adapter);
            adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
                public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                    if (view.getId() == R.id.textView_name) {
                        String path = cursor.getString(columnIndex);
                        ((TextView) view).setText(path.substring(path.lastIndexOf("/") + 1));
                        return true;
                    } else if (view.getId() == R.id.textView_size) {
                        long filesize = cursor.getLong(columnIndex);
                        ((TextView) view).setText(Formatter.formatFileSize(CategoryActivity.this, filesize));
                        return true;
                    } else if (view.getId() == R.id.textView_time) {
                        Date date = new Date(cursor.getLong(columnIndex) * 1000);
                        ((TextView) view).setText(SDF.format(date));
                        return true;
                    } else if (view.getId() == R.id.imageView_icon) {
                        ((ImageView) view).setImageResource(R.mipmap.pdffile);
                        return true;
                    }
                    return false;
                }
            });
            listView_pdf.setAdapter(adapter);
            listView_pdf.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    String path = ((TextView) arg1.findViewById(R.id.textView_path)).getText().toString();
                    String mime = URLConnection.getFileNameMap().getContentTypeFor(path);
                    if (mime != null) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.fromFile(new File(path)), mime);
                        startActivity(intent);
                    }
                }
            });
            listView_pdf.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
                @Override
                public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
                    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
                    filepath = ((TextView) info.targetView.findViewById(R.id.textView_path)).getText().toString();
                    String title = ((TextView) info.targetView.findViewById(R.id.textView_name)).getText().toString();
                    menu.setHeaderTitle(title);
                    menu.add(0, 0, 0, "复制");
                    menu.add(0, 1, 1, "剪切");
                    menu.add(0, 2, 2, "重命名");
                    menu.add(0, 3, 3, "属性");
                    menu.add(0, 4, 4, "分享");
                    menu.add(0, 5, 5, "删除");
                    menu.add(0, 6, 6, "打开文件所在位置");
                }
            });

            ListView listView_xls = findViewById(R.id.listView_xls);
            cursor1 = getContentResolver().query(MediaStore.Files.getContentUri("external"), projection, MediaStore.Files.FileColumns.MIME_TYPE + "=?", new String[]{ "application/vnd.ms-excel" }, MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC");
            adapter = new SimpleCursorAdapter(this, R.layout.item, cursor1, new String[]{ MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.SIZE, MediaStore.Files.FileColumns.DATE_MODIFIED, MediaStore.Files.FileColumns._ID }, new int[]{ R.id.textView_path, R.id.textView_name, R.id.textView_size, R.id.textView_time, R.id.imageView_icon }, CursorAdapter.FLAG_AUTO_REQUERY);
            list_cursor.add(cursor1);
            list_adapter.add(adapter);
            adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
                public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                    if (view.getId() == R.id.textView_name) {
                        String path = cursor.getString(columnIndex);
                        ((TextView) view).setText(path.substring(path.lastIndexOf("/") + 1));
                        return true;
                    } else if (view.getId() == R.id.textView_size) {
                        long filesize = cursor.getLong(columnIndex);
                        ((TextView) view).setText(Formatter.formatFileSize(CategoryActivity.this, filesize));
                        return true;
                    } else if (view.getId() == R.id.textView_time) {
                        Date date = new Date(cursor.getLong(columnIndex) * 1000);
                        ((TextView) view).setText(SDF.format(date));
                        return true;
                    } else if (view.getId() == R.id.imageView_icon) {
                        ((ImageView) view).setImageResource(R.mipmap.xlsfile);
                        return true;
                    }
                    return false;
                }
            });
            listView_xls.setAdapter(adapter);
            listView_xls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    String path = ((TextView) arg1.findViewById(R.id.textView_path)).getText().toString();
                    String mime = URLConnection.getFileNameMap().getContentTypeFor(path);
                    if (mime != null) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.fromFile(new File(path)), mime);
                        startActivity(intent);
                    }
                }
            });
            listView_xls.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
                @Override
                public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
                    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
                    filepath = ((TextView) info.targetView.findViewById(R.id.textView_path)).getText().toString();
                    String title = ((TextView) info.targetView.findViewById(R.id.textView_name)).getText().toString();
                    menu.setHeaderTitle(title);
                    menu.add(0, 0, 0, "复制");
                    menu.add(0, 1, 1, "剪切");
                    menu.add(0, 2, 2, "重命名");
                    menu.add(0, 3, 3, "属性");
                    menu.add(0, 4, 4, "分享");
                    menu.add(0, 5, 5, "删除");
                    menu.add(0, 6, 6, "打开文件所在位置");
                }
            });

            ListView listView_ppt = findViewById(R.id.listView_ppt);
            cursor1 = getContentResolver().query(MediaStore.Files.getContentUri("external"), projection, MediaStore.Files.FileColumns.MIME_TYPE + "=?", new String[]{ "application/vnd.ms-powerpoint" }, MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC");
            adapter = new SimpleCursorAdapter(this, R.layout.item, cursor1, new String[]{ MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.SIZE, MediaStore.Files.FileColumns.DATE_MODIFIED, MediaStore.Files.FileColumns._ID }, new int[]{ R.id.textView_path, R.id.textView_name, R.id.textView_size, R.id.textView_time, R.id.imageView_icon }, CursorAdapter.FLAG_AUTO_REQUERY);
            list_cursor.add(cursor1);
            list_adapter.add(adapter);
            adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
                public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                    if (view.getId() == R.id.textView_name) {
                        String path = cursor.getString(columnIndex);
                        ((TextView) view).setText(path.substring(path.lastIndexOf("/") + 1));
                        return true;
                    } else if (view.getId() == R.id.textView_size) {
                        long filesize = cursor.getLong(columnIndex);
                        ((TextView) view).setText(Formatter.formatFileSize(CategoryActivity.this, filesize));
                        return true;
                    } else if (view.getId() == R.id.textView_time) {
                        Date date = new Date(cursor.getLong(columnIndex) * 1000);
                        ((TextView) view).setText(SDF.format(date));
                        return true;
                    } else if (view.getId() == R.id.imageView_icon) {
                        ((ImageView) view).setImageResource(R.mipmap.pptfile);
                        return true;
                    }
                    return false;
                }
            });
            listView_ppt.setAdapter(adapter);
            listView_ppt.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    String path = ((TextView) arg1.findViewById(R.id.textView_path)).getText().toString();
                    String mime = URLConnection.getFileNameMap().getContentTypeFor(path);
                    if (mime != null) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.fromFile(new File(path)), mime);
                        startActivity(intent);
                    }
                }
            });
            listView_ppt.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
                @Override
                public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
                    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
                    filepath = ((TextView) info.targetView.findViewById(R.id.textView_path)).getText().toString();
                    String title = ((TextView) info.targetView.findViewById(R.id.textView_name)).getText().toString();
                    menu.setHeaderTitle(title);
                    menu.add(0, 0, 0, "复制");
                    menu.add(0, 1, 1, "剪切");
                    menu.add(0, 2, 2, "重命名");
                    menu.add(0, 3, 3, "属性");
                    menu.add(0, 4, 4, "分享");
                    menu.add(0, 5, 5, "删除");
                    menu.add(0, 6, 6, "打开文件所在位置");
                }
            });
        } else if (category.equals("Download")) {
            setTitle("下载");
            String[] projection = new String[]{ MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.DATE_MODIFIED, MediaStore.Files.FileColumns.SIZE, MediaStore.Files.FileColumns._ID };
            Cursor cursor1 = getContentResolver().query(MediaStore.Files.getContentUri("external"), projection, MediaStore.Files.FileColumns.DATA + " like ?", new String[]{ "%" + Environment.getExternalStorageDirectory().getPath() + File.separator + Environment.DIRECTORY_DOWNLOADS + File.separator + "%" }, MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC");
            ListView listView = new ListView(this);
            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor1, new String[]{ MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.SIZE, MediaStore.Files.FileColumns.DATE_MODIFIED, MediaStore.Files.FileColumns._ID }, new int[]{ R.id.textView_path, R.id.textView_name, R.id.textView_size, R.id.textView_time, R.id.imageView_icon }, CursorAdapter.FLAG_AUTO_REQUERY);
            list_cursor.add(cursor1);
            list_adapter.add(adapter);
            adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
                public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                    if (view.getId() == R.id.textView_name) {
                        String path = cursor.getString(columnIndex);
                        ((TextView) view).setText(path.substring(path.lastIndexOf("/") + 1));
                        return true;
                    } else if (view.getId() == R.id.textView_size) {
                        long filesize = cursor.getLong(columnIndex);
                        ((TextView) view).setText(Formatter.formatFileSize(CategoryActivity.this, filesize));
                        return true;
                    } else if (view.getId() == R.id.textView_time) {
                        Date date = new Date(cursor.getLong(columnIndex) * 1000);
                        ((TextView) view).setText(SDF.format(date));
                        return true;
                    } else if (view.getId() == R.id.imageView_icon) {
                        int id = cursor.getInt(columnIndex);
                        String path = queryPathByID(id);
                        String mime = URLConnection.getFileNameMap().getContentTypeFor(path);
                        Log.e(Thread.currentThread().getStackTrace()[2] + "", id + " -> " + path + " -> " + mime);
                        if (mime != null) {
                            if (mime.startsWith("image/")) {
                                Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), id, MediaStore.Images.Thumbnails.MINI_KIND, null);
                                ((ImageView) view).setImageBitmap(bitmap);
                            } else if (mime.startsWith("video/")) {
                                Bitmap bitmap = MediaStore.Video.Thumbnails.getThumbnail(getContentResolver(), id, MediaStore.Video.Thumbnails.MINI_KIND, null);
                                ((ImageView) view).setImageBitmap(bitmap);
                            } else if (mime.startsWith("audio/")) {
                                ((ImageView) view).setImageResource(R.mipmap.audiofile);
                            } else if (mime.equals("text/plain")) {
                                ((ImageView) view).setImageResource(R.mipmap.txtfile);
                            } else if (mime.equals("text/html")) {
                                ((ImageView) view).setImageResource(R.mipmap.htmlfile);
                            } else if (mime.equals("application/msword")) {
                                ((ImageView) view).setImageResource(R.mipmap.docfile);
                            } else if (mime.equals("application/pdf")) {
                                ((ImageView) view).setImageResource(R.mipmap.pdffile);
                            } else if (mime.equals("application/x-tar") || mime.equals("application/zip") || mime.equals("application/x-gzip")) {
                                ((ImageView) view).setImageResource(R.mipmap.zipfile);
                            } else {
                                ((ImageView) view).setImageResource(R.mipmap.file);
                            }
                        } else {
                            ((ImageView) view).setImageResource(R.mipmap.file);
                        }
                        return true;
                    }
                    return false;
                }
            });
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    String path = ((TextView) arg1.findViewById(R.id.textView_path)).getText().toString();
                    String mime = URLConnection.getFileNameMap().getContentTypeFor(path);
                    Log.e(Thread.currentThread().getStackTrace()[2] + ": ", path + " -> " + mime);
                    if (mime != null) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.fromFile(new File(path)), mime);
                        startActivity(intent);
                    }
                }
            });
            listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
                @Override
                public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
                    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
                    filepath = ((TextView) info.targetView.findViewById(R.id.textView_path)).getText().toString();
                    String title = ((TextView) info.targetView.findViewById(R.id.textView_name)).getText().toString();
                    menu.setHeaderTitle(title);
                    menu.add(0, 0, 0, "复制");
                    menu.add(0, 1, 1, "剪切");
                    menu.add(0, 2, 2, "重命名");
                    menu.add(0, 3, 3, "属性");
                    menu.add(0, 4, 4, "分享");
                    menu.add(0, 5, 5, "删除");
                    menu.add(0, 6, 6, "打开文件所在位置");
                }
            });
            linearLayout_main.addView(listView);
        } else if (category.equals("APK")) {
            setTitle("安装包");
            String[] projection = new String[]{ MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.DATE_MODIFIED, MediaStore.Files.FileColumns.SIZE, MediaStore.Files.FileColumns._ID };
            Cursor cursor1 = getContentResolver().query(MediaStore.Files.getContentUri("external"), projection, MediaStore.Files.FileColumns.MIME_TYPE + "=?", new String[]{ "application/vnd.android.package-archive" }, MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC");
            ListView listView = new ListView(this);
            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor1, new String[]{ MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.SIZE, MediaStore.Files.FileColumns.DATE_MODIFIED, MediaStore.Files.FileColumns._ID }, new int[]{R.id.textView_path, R.id.textView_name, R.id.textView_size, R.id.textView_time, R.id.imageView_icon}, CursorAdapter.FLAG_AUTO_REQUERY);
            list_cursor.add(cursor1);
            list_adapter.add(adapter);
            adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
                public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                    if (view.getId() == R.id.textView_name) {
                        String path = cursor.getString(columnIndex);
                        ((TextView) view).setText(path.substring(path.lastIndexOf("/") + 1));
                        return true;
                    } else if (view.getId() == R.id.textView_size) {
                        long filesize = cursor.getLong(columnIndex);
                        ((TextView) view).setText(Formatter.formatFileSize(CategoryActivity.this, filesize));
                        return true;
                    } else if (view.getId() == R.id.textView_time) {
                        Date date = new Date(cursor.getLong(columnIndex) * 1000);
                        ((TextView) view).setText(SDF.format(date));
                        return true;
                    } else if (view.getId() == R.id.imageView_icon) {
                        ((ImageView) view).setImageResource(R.mipmap.apk);
                        return true;
                    }
                    return false;
                }
            });
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    String path = ((TextView) arg1.findViewById(R.id.textView_path)).getText().toString();
                    String mime = URLConnection.getFileNameMap().getContentTypeFor(path);
                    if (mime != null) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.fromFile(new File(path)), mime);
                        startActivity(intent);
                    }
                }
            });
            listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
                @Override
                public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
                    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
                    filepath = ((TextView) info.targetView.findViewById(R.id.textView_path)).getText().toString();
                    String title = ((TextView) info.targetView.findViewById(R.id.textView_name)).getText().toString();
                    menu.setHeaderTitle(title);
                    menu.add(0, 0, 0, "复制");
                    menu.add(0, 1, 1, "剪切");
                    menu.add(0, 2, 2, "重命名");
                    menu.add(0, 3, 3, "属性");
                    menu.add(0, 4, 4, "分享");
                    menu.add(0, 5, 5, "删除");
                    menu.add(0, 6, 6, "打开文件所在位置");
                }
            });
            linearLayout_main.addView(listView);
        } else if (category.equals("ZIP")) {
            setTitle("压缩包");
            String[] projection = new String[]{ MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.DATE_MODIFIED, MediaStore.Files.FileColumns.SIZE, MediaStore.Files.FileColumns._ID };
            Cursor cursor1 = getContentResolver().query(MediaStore.Files.getContentUri("external"), projection, MediaStore.Files.FileColumns.MIME_TYPE + "=? or " +  MediaStore.Files.FileColumns.MIME_TYPE + "=? or " +  MediaStore.Files.FileColumns.MIME_TYPE + "=?", new String[]{ "application/x-tar", "application/zip", "application/x-gzip" }, MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC");
            ListView listView = new ListView(this);
            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor1, new String[]{ MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.SIZE, MediaStore.Files.FileColumns.DATE_MODIFIED, MediaStore.Files.FileColumns._ID }, new int[]{R.id.textView_path, R.id.textView_name, R.id.textView_size, R.id.textView_time, R.id.imageView_icon}, CursorAdapter.FLAG_AUTO_REQUERY);
            list_cursor.add(cursor1);
            list_adapter.add(adapter);
            adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
                public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                    if (view.getId() == R.id.textView_name) {
                        String path = cursor.getString(columnIndex);
                        ((TextView) view).setText(path.substring(path.lastIndexOf("/") + 1));
                        return true;
                    } else if (view.getId() == R.id.textView_size) {
                        long filesize = cursor.getLong(columnIndex);
                        ((TextView) view).setText(Formatter.formatFileSize(CategoryActivity.this, filesize));
                        return true;
                    } else if (view.getId() == R.id.textView_time) {
                        Date date = new Date(cursor.getLong(columnIndex) * 1000);
                        ((TextView) view).setText(SDF.format(date));
                        return true;
                    } else if (view.getId() == R.id.imageView_icon) {
                        ((ImageView) view).setImageResource(R.mipmap.zipfile);
                        return true;
                    }
                    return false;
                }
            });
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    String path = ((TextView) arg1.findViewById(R.id.textView_path)).getText().toString();
                    String mime = URLConnection.getFileNameMap().getContentTypeFor(path);
                    if (mime != null) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.fromFile(new File(path)), mime);
                        startActivity(intent);
                    }
                }
            });
            listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
                @Override
                public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
                    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
                    filepath = ((TextView) info.targetView.findViewById(R.id.textView_path)).getText().toString();
                    String title = ((TextView) info.targetView.findViewById(R.id.textView_name)).getText().toString();
                    menu.setHeaderTitle(title);
                    menu.add(0, 0, 0, "复制");
                    menu.add(0, 1, 1, "剪切");
                    menu.add(0, 2, 2, "重命名");
                    menu.add(0, 3, 3, "属性");
                    menu.add(0, 4, 4, "分享");
                    menu.add(0, 5, 5, "删除");
                    menu.add(0, 6, 6, "打开文件所在位置");
                }
            });
            linearLayout_main.addView(listView);
        }
    }

    String queryPathByID(int id) {
        Uri uri = MediaStore.Files.getContentUri("external");
        String[] projection = new String[] { MediaStore.Files.FileColumns.DATA };
        String selection = MediaStore.Files.FileColumns._ID + "=?";
        String[] selectionArgs = new String[] { String.valueOf(id) };
        Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, null);
        String path = "";
        if (cursor.moveToFirst()) {
            path = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA));
        }
        cursor.close();
        //Log.e(Thread.currentThread().getStackTrace()[2] + "", id + ", " + path);
        return path;
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        //AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        String filename = filepath.substring(filepath.lastIndexOf(File.separator) + 1);
        switch (item.getItemId()) {
            case 0:
                Toast.makeText(getApplicationContext(), "复制", Toast.LENGTH_SHORT).show();
                break;
            case 1:
                Toast.makeText(getApplicationContext(), "剪切", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                final EditText editText = new EditText(CategoryActivity.this);
                editText.setText(filename);
                AlertDialog.Builder dialog = new AlertDialog.Builder(CategoryActivity.this);
                dialog.setTitle("重命名").setView(editText);
                dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String path1 = filepath.substring(0, filepath.lastIndexOf(File.separator)) + File.separator + editText.getText().toString();
                        File file = new File(filepath);
                        boolean b = file.renameTo(new File(path1));
                        if (b) {
                            ContentValues values = new ContentValues();
                            values.put(MediaStore.Files.FileColumns.DATA, path1);
                            getContentResolver().update(MediaStore.Files.getContentUri("external"), values, MediaStore.Files.FileColumns.DATA + "=?", new String[]{ filepath });
                            for (int i=0; i<list_adapter.size(); i++) {
                                list_cursor.get(i).requery();
                                list_adapter.get(i).notifyDataSetChanged();
                            }
                        } else {
                            Toast.makeText(CategoryActivity.this, "重命名失败:\n" + path1, Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).show();
                break;
            case 3:
                File file = new File(filepath);
                String mime = URLConnection.getFileNameMap().getContentTypeFor(filepath);
                Date datec = new Date(file.lastModified());
                new AlertDialog.Builder(this).setIcon(R.mipmap.ic_launcher).setTitle("属性").setMessage("文件路径：" + filepath + "\n创建时间：" + SDF.format(datec) + "\n类型：" + mime + "\n大小：" + Formatter.formatFileSize(this, file.length())).setPositiveButton("确定", null).show();
                break;
            case 4:
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_SEND);
                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filepath)));
                intent.setType("*/*");
                startActivity(Intent.createChooser(intent, "分享 " + filename));
                break;
            case 5:
                file = new File(filepath);
                if (file.exists()) {
                    boolean b = file.delete();
                    if (b) {
                        getContentResolver().delete(MediaStore.Files.getContentUri("external"), MediaStore.Files.FileColumns.DATA + "=?", new String[]{ filepath });
                        //刷新
                        //方法1：文档每个分类共用一个cursor1，这时候的cursor1是最后一个，就不行了。
                        //cursor1.requery();
                        //adapter.notifyDataSetChanged();
                        //方法2：界面会跳动
                        //intent = getIntent();
                        //finish();
                        //startActivity(intent);
                        //方法3：加入列表遍历方法1
                        for (int i=0; i<list_adapter.size(); i++) {
                            list_cursor.get(i).requery();
                            list_adapter.get(i).notifyDataSetChanged();
                        }
                    } else {
                        Toast.makeText(CategoryActivity.this, "删除失败！", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    getContentResolver().delete(MediaStore.Files.getContentUri("external"), MediaStore.Files.FileColumns.DATA + "=?", new String[]{ filepath });
                }
                break;
            case 6:
                intent = new Intent(CategoryActivity.this, MainActivity.class);
                intent.putExtra("Path", filepath);
                startActivity(intent);
                break;
            case 7:
                intent = new Intent(CategoryActivity.this, TexteditActivity.class);
                intent.putExtra("Path", filepath);
                startActivity(intent);
                break;
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}